// pages/menu/advertise/advertise.js
var app = getApp()
var verify = require('../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    size: 10,
    loading: true,
    filterValue: "",
    isFresh: false,
    inputShowed: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth
    })
  },

  // 获取广告模板
  getAdList(){
    var size = this.data.size
    var filterValue = this.data.filterValue
    var token = app.globalData.token
    var baseUrl = app.globalData.baseUrl
    var that = this

    wx.request({
      url: baseUrl + 'advertising/advertisingStrategyList2?productModel=ALL&filter=strategyName&page=0&size=' + size + '&filterValue=' + filterValue,
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          // console.log("advertiseList = ",res.data.data.content)
          var advertiseList = res.data.data.content
          var totalElements = res.data.data.totalElements
          var loading = false
          if (totalElements > size) {
            loading = true
          }
          that.setData({
            advertiseList: advertiseList,
            totalElements: totalElements,
            loading: loading,
            isFresh: false
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },

  // 搜索相关
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  // 取消搜索
  hideInput: function () {
    this.setData({
      size: 10,
      loading: true,
      filterValue: "",
      isFresh: false,
      inputShowed: false,
    });
    this.getAdList();
  },
  inputTyping: function (e) {
    this.setData({
      filterValue: e.detail.value
    });
    this.getAdList();
  },
  // 搜索框输入
  input: function (res) {
    this.setData({
      filterValue: res.detail.value
    })
  },
  
  // 查看广告详情
  adDetail(res){
    var index = res.currentTarget.dataset.index;
    var advertise = this.data.advertiseList[index]
    var advertisingStrategyProperty = this.data.advertiseList[index].advertisingStrategyProperty;
    var advertisingTempName = this.data.advertiseList[index].advertisingTempName;
    var strategyName = this.data.advertiseList[index].strategyName;
    var productType = this.data.advertiseList[index].productType;
    var productModel = this.data.advertiseList[index].productModel;
    var remark = this.data.advertiseList[index].remark;
    var indenfier = this.data.advertiseList[index].indenfier;
    wx.navigateTo({
      url: 'ad-detail/ad-detail?advertisingStrategyProperty=' + advertisingStrategyProperty
        + '&advertisingTempName=' + advertisingTempName
        + '&strategyName=' + strategyName
        + '&productType=' + productType
        + '&productModel=' + productModel
        + '&remark=' + remark
        + '&indenfier=' + indenfier,
    })
    
  },

  // 绑定设备
  bindDevice(res){
    var index = res.currentTarget.dataset.index
    var id = this.data.advertiseList[index].id
    // console.log("index = ", res)
    wx.navigateTo({
      url: 'bindDevice/bindDevice?id='+id,
    })
  },

  // 发布
  publish(res){
    var index = res.currentTarget.dataset.index
    var id = this.data.advertiseList[index].id
    var token = app.globalData.token
    var baseUrl = app.globalData.baseUrl
    var that = this

    wx.request({
      url: baseUrl + 'advertising/issueAdvertising',
      header: {
        'AUTHORIZATION': token
      },
      data:{
        advertisingStrategyId:id
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          wx.showToast({
            title: '发布成功!',
            duration: 3500
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function () {
    this.getAdList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var size = this.data.size * 1 + 10
    var loading = this.data.loading
    var totalElements = this.data.totalElements
    this.setData({
      isFresh: true
    })
    if (size - 10 > totalElements) {
      this.setData({
        loading: false
      })
    } else {
      this.setData({
        size: size
      })
      this.getAdList()
    }
  }
})